﻿using MPI;
using ParallelLib.Micro;
using System;

namespace ParallelLib
{
    partial class Program
    {
        public class MpiWorker
        {
            private Poller _poller;
            private MicroService service;
            private int Rank;
            private Intracommunicator World = Intracommunicator.world;
            private Intracommunicator Self = Intracommunicator.self;
            public MpiWorker()
            {
                _poller = new Poller(PollerWork, "MpiWorker");
            }

            public void StartService()
            {
                service = new MicroService("MpiWorker", 12888);
                service.InitNode();
                service.StartListenMsg();
                service.QueryAllNode();
                service.OnReceivedMsg += Service_OnReceivedMsg;
            }

            public void StartWork()
            {
                StartPoller();
            }

            private void Service_OnReceivedMsg(object sender, MsgArgs e)
            {
                
            }

            public void StartPoller()
            {
                _poller.Start();
            }
            public void StopPoller()
            {
                _poller.Stop();
            }

            public void PausePoller()
            {
                _poller.Pause();
            }
            public void ContinuePoller()
            {
                _poller.Continue();
            }
            private void PollerWork()
            {

            }

            private void InitByRank(int rank)
            {
                Rank = rank;
                if (Rank == 0)
                {
                    //TODO:根节点
                }
            }
            private string[] Args;
            private void SaveArgs(string[] args)
            {
                if (args == null)
                    Args = null;
                Args = new string[args.Length];
                for (int i = 0; i < args.Length; i++)
                {
                    Args[i] = args[i];
                }
            }

            public void RunMpi(string[] args)
            {
                SaveArgs(args);
                MPI.Environment.Run(ref args, MpiWork);
            }

            public void MpiWork(Intracommunicator comm)
            {
                InitByRank(comm.Rank);
                if (comm.Rank == 0)
                {
                    Console.WriteLine("Rank 0 is alive and running on " + MPI.Environment.ProcessorName);
                    while (true)
                    {
                        Console.WriteLine("Ready Input CMD...\n" + comm.Size + "\n");
                        var cmd = Console.ReadLine();
                        SendCmd(comm, cmd);
                        if (cmd == "exit")
                            break;
                    }
                }
                else
                {
                    while (true)
                    {
                        Console.WriteLine("Waiting CMD...");
                        var str = comm.Receive<string>(0, 0);
                        Debug("{0}-收到消息:{1}", MPI.Environment.ProcessorName, str);
                        switch (str)
                        {
                            case "exit":
                                comm.Send(MPI.Environment.ProcessorName + "_Exit", 0, 1);
                                break;
                            default:
                                var msg = MPI.Environment.ProcessorName + "****已收到消息" + str;
                                comm.Send(msg, 0, 1);
                                Debug("{0}-已发送消息:【{1}】", MPI.Environment.ProcessorName, msg);
                                break;
                        }
                    }
                }
            }
        }
    }
}
